package com.situ.mall.controller;

import com.situ.common.util.Result;
import com.situ.mall.pojo.entity.User;
import com.situ.mall.pojo.query.UserQuery;
import com.situ.mall.service.IUserService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @RequestMapping("/checkUserLogin")
    @ResponseBody
    public Result checkUserLogin(HttpSession session) {
        User user = (User) session.getAttribute("user");
        return user != null ? Result.ok() : Result.error();
    }
    @RequestMapping("/checkUserLoginTop")
    @ResponseBody
    public Result checkUserLoginTop(HttpSession session) {
        User user = (User) session.getAttribute("user");
        return user != null ? Result.ok(user) : Result.error();
    }
    @RequestMapping("/login")
    @ResponseBody
    public Result login(UserQuery userQuery, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(userQuery.getCode())) {
            return Result.error("验证码错误");
        }
        User user = userService.login(userQuery.getAccount(), userQuery.getPassword());
        if (user == null) {
            return Result.error("用户名或密码错误");
        }
        if (user.getStatus() == 0) {
            return Result.error("该用户已经禁用，请联系管理员");
        }
        if (!ObjectUtils.isEmpty(userQuery.getAutoLogin())) {
            Cookie cookie = new Cookie("autoLoginPortal", user.getAccount() + "&" + user.getPassword());
            //设置Cookie 的存活时间和绑定路径
            cookie.setMaxAge(60*60*24*7);//设置时间为1周
            //cookie.setPath(req.getContextPath());
            //在响应resp中添加cookie，返回给浏览器
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        session.setAttribute("user", user);
        return Result.ok("登录成功");
    }
    @RequestMapping("/logout")
    @ResponseBody
    public Result logout(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        Cookie[] cookies = request.getCookies();
        Cookie cookie = null;
        // 判断cookie是否为空
        if (!ObjectUtils.isEmpty(cookies)) {
            for (Cookie C : cookies) {
                // 获取cookie的name
                String name = C.getName();
                if ("autoLoginPortal".equals(name)) {
                    cookie = C;
                    break;

                }
            }
        }
        if (!ObjectUtils.isEmpty(cookie)) {
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return Result.ok("退出成功");
    }
    @RequestMapping("/selectByInfoId")
    @ResponseBody
    public Result selectByInfoId(HttpSession session) {
        User user = (User) session.getAttribute("user");
        User userInfo = userService.selectByInfoId(user.getId());
        return Result.ok(userInfo);
    }
    @RequestMapping("/updateInfo")
    @ResponseBody
    public Result updateInfo(User user, HttpSession session) {
        Integer accountCount = userService.selectByAccountId(user);
        if (accountCount == 0) {
            userService.updateInfo(user);
            session.setAttribute("user", user);
            return Result.ok("修改成功！");
        } else {
            return Result.error("账号已经存在！");
        }

    }
    @RequestMapping("/updatePassword")
    @ResponseBody
    public Result updatePassword(HttpServletRequest request, HttpServletResponse response, User user, String newPassword, String confirmPassword) {
        User userInfo = userService.selectByInfoId(user.getId());
        if (Objects.equals(user.getPassword(), userInfo.getPassword())) {
            userService.updatePassword(user.getId(), newPassword);
            HttpSession session = request.getSession();
            session.removeAttribute("user");
            Cookie[] cookies = request.getCookies();
            Cookie cookie = null;
            // 判断cookie是否为空
            if (!ObjectUtils.isEmpty(cookies)) {
                for (Cookie C : cookies) {
                    // 获取cookie的name
                    String name = C.getName();
                    if ("autoLogin".equals(name)) {
                        cookie = C;
                        break;

                    }
                }
            }
            if (!ObjectUtils.isEmpty(cookie)) {
                cookie.setMaxAge(0);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
            return Result.ok("退出成功");
        } else {
            return Result.error("原密码错误！");
        }

    }
    @RequestMapping("/register")
    @ResponseBody
    public Result register(User user, Integer vercode) {
        if (vercode == 1234) {
            Integer accountCount = userService.selectByAccount(user);
            if (accountCount == 0) {
                userService.register(user);
                return Result.ok("注册成功，请登录！");
            } else {
                return Result.error("账号已经存在！");
            }
        } else {
            return Result.error("验证码错误！");
        }
    }
    @RequestMapping("/forget")
    @ResponseBody
    public Result forget(User user, Integer vercode) {
        if (vercode == 1234) {
            Integer accountCount = userService.selectByAccountPhone(user);
            if (accountCount == 1) {
                userService.forget(user);
                return Result.ok("修改成功，请登录！");
            } else {
                return Result.error("账号或手机号不正确！");
            }
        } else {
            return Result.error("验证码错误！");
        }
    }
}
